package com.ruoyi.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.domain.Ability_BlackListInfor;
import com.ruoyi.mapper.Ability_BlackListInforMapper;
import com.ruoyi.service.Ability_BlackListInforService;
import com.ruoyi.service.BlackListKong;
import com.ruoyi.utils.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Pattern;

@Service("Ability_BlackListInforServiceImpl")
public class Ability_BlackListInforServiceImpl implements Ability_BlackListInforService {
    @Autowired
    Ability_BlackListInforMapper ability_BlackListInforMapper;
    @Autowired
    BlackListKong blackListKong;

   private static final String IP_PATTERN = "^((\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5])\\.){3}(\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5])$";
    @Override
    public int insertAbility_BlackListInfor(Ability_BlackListInfor ability_BlackListInfor) {
       if( Pattern.matches(IP_PATTERN, ability_BlackListInfor.getIp())){
           Ability_BlackListInfor infor = ability_BlackListInforMapper.selectAbility_BlackListInforByIp(ability_BlackListInfor.getIp());
           if(infor!=null){
               return -102;
           }

           String response =  "";
           try {
               response = blackListKong.addBlackList(ability_BlackListInfor.getIp());
           } catch (Exception e){
               e.printStackTrace();
               return -100;
           }
           int k=ability_BlackListInforMapper.insertAbility_BlackListInfor(ability_BlackListInfor);
           return k;
       }else {
           return -101;
       }

    }

    @Override
    public int deleteAbility_BlackListInforById(Integer id) {
        Ability_BlackListInfor ability_BlackListInfor = ability_BlackListInforMapper.selectAbility_BlackListInforById(id);
        String response =  "";
        try {
            response = blackListKong.delBlackList(ability_BlackListInfor.getIp());
        } catch (Exception e){
            e.printStackTrace();
            return -100;
        }
        int k=ability_BlackListInforMapper.deleteAbility_BlackListInforById(id);
        return k;
    }

    @Override
    public List<Ability_BlackListInfor> selectAbility_BlackListInforList(Ability_BlackListInfor ability_BlackListInfor) {
        return ability_BlackListInforMapper.selectAbility_BlackListInforList(ability_BlackListInfor);
    }

    @Override
    public Ability_BlackListInfor selectAbility_BlackListInforById(Integer id) {
        return ability_BlackListInforMapper.selectAbility_BlackListInforById(id);
    }
}
